package Entity;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import Control.CommonControls;
import Control.MySQLController;
import Entity.Shop_Product_Catalog_StatusCode;

public class Shop_Catalog {
	
	private String id;
	private String name;
	private String description;
	private Date createDate;
	private Date updateDate;
	private Shop_Product_Catalog_StatusCode statusCode;
	
	public Shop_Catalog(String id){
		this.id = id;
	}
	
	public Shop_Catalog(String id, String name, String description, Date createDate, Date updateDate, String statusCode){
		this.id = id;
		this.name = name;
		this.description = description;
		this.createDate = createDate;
		this.updateDate = updateDate;
		this.statusCode = new Shop_Product_Catalog_StatusCode(statusCode);
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	public Date getUpdateDate() {
		return updateDate;
	}
	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}
	public Shop_Product_Catalog_StatusCode getStatusCode() {
		return statusCode;
	}
	public void setStatusCode(Shop_Product_Catalog_StatusCode statusCode) {
		this.statusCode = statusCode;
	}
	
	public void getCatalogDetailsById(){
		if(description == null){
			MySQLController db = new MySQLController();
			db.getConnection();
			String sql = "SELECT * FROM ooadpj.catalog WHERE catalog_id = '" + id + "'";
			ResultSet rs = null;
			rs = db.readRequest(sql);
			try{
				while(rs.next()){
					setName(rs.getString("name"));
					setDescription(rs.getString("description"));
					setCreateDate(rs.getDate("create_date"));
					setUpdateDate(rs.getDate("update_date"));
					setStatusCode(new Shop_Product_Catalog_StatusCode(rs.getString("catalog_status_code")));
				}
			}
			catch(Exception e){
				e.printStackTrace();
			}
			finally{
				db.terminate();
			}
		}
	}
	
	public static ArrayList<Shop_Catalog> retrieveAllCatalogs(){
		ArrayList<Shop_Catalog> catalogList = new ArrayList<Shop_Catalog>();
		
		ResultSet rs = null;
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.catalog";
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				Shop_Catalog catalog = new Shop_Catalog(rs.getString("catalog_id"), rs.getString("name"), rs.getString("description"), rs.getDate("create_date"), rs.getDate("update_date"), rs.getString("catalog_status_code"));
				catalogList.add(catalog);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return catalogList;
	}
	
	public boolean createCatalog(){
		boolean success = false;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "INSERT INTO ooadpj.catalog(catalog_id, name, description, create_date, catalog_status_code) VALUES ('" + id + "', '" + name + "', '" + description + "', '" + sdf.format(CommonControls.getCurrentDate()) + "', 'AVA')";
		if(db.updateRequest(sql) == 1){
			success = true;
		}
		db.terminate();
		return success;
	}

}
